<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Uploader HTML5 Tests</title>
<script type="text/javascript" src="../../../../build/yui/yui.js"></script>
<style>
.testclass {
    color: #ff0000;
}
</style>
</head>

<body class="yui3-skin-sam">
<h1>HTML5 Uploader Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
<div id="container"></div>
<div id="droparea"></div>
<div id="secondroparea"></div>
<script type="text/javascript">

(function() {

    YUI({
        coverage: ['uploader-html5'],
        filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min'
    }).use("console", "test", "dump", "uploader-html5", function(Y) {



        // Set up the page
        var ASSERT = Y.Assert,
            ARRAYASSERT = Y.ArrayAssert,
            btnRun = Y.one("#btnRun"),
            myConsole = new Y.Console().render();

        btnRun.set("disabled", false);
        Y.on("click", function(){
            Y.Test.Runner.run();
        }, btnRun);


        var BasicUploader = new Y.Test.Case({
            name: "Basic Uploader HTML5 Tests",

            setUp : function () {

                var basicuploader = "<div id='basicuploader'></div>";
                Y.one('#container').appendChild(Y.Node.create(basicuploader));
                this.basicuploader = null;

            },

            tearDown : function () {
               delete this.basicuploader;
               Y.one('#basicuploader').remove();
            },


            testUploaderInit : function() {
                var cfg = {
                  tabIndex: "0",
                  width: "100%",
                  height: "100%"
                };

                this.basicuploader = new Y.UploaderHTML5(cfg);
                this.basicuploader.render("#basicuploader");

                Y.Assert.areEqual(this.basicuploader.get('visible'), true);
            },

            testDropArea : function() {
                var cfg = {
                  tabIndex: "0",
                  width: "100%",
                  height: "100%",
                  dragAndDropArea: "#droparea"
                };

                this.basicuploader = new Y.UploaderHTML5(cfg);
                this.basicuploader.render("#basicuploader");

                Y.Assert.areEqual(this.basicuploader.get('dragAndDropArea'), Y.one("#droparea"));

                this.basicuploader.set("dragAndDropArea", "#seconddroparea");
                Y.Assert.areEqual(this.basicuploader.get('dragAndDropArea'), Y.one("#seconddroparea"));

                var e = new Y.DOMEventFacade({type:'dragenter', preventDefault: function () {}, stopPropagation: function () {}});

                Y.FileHTML5.prototype.isValidFile = function () { return true; };
                Y.FileHTML5.prototype.canUpload = function () { return true; };

                var filearray = [];

                for (var i = 0; i <= 10; i++) {
                    filearray.push({name: i + ".jpg", size: i*1000, type: "jpg"});
                }

                e._event.dataTransfer = {files: filearray};
                e._event.dataTransfer.types = ["Files"];

                this.basicuploader._ddEventHandler(e);

                e.type = 'dragleave';

                this.basicuploader._ddEventHandler(e);

                e.type = 'dragover';

                this.basicuploader._ddEventHandler(e);

                e.type = 'drop';

                this.basicuploader._ddEventHandler(e);
            },

            testEnabled : function () {
                var cfg = {
                  tabIndex: "0",
                  width: "100%",
                  height: "100%",
                  dragAndDropArea: "#droparea",
                  enabled: true
                };

                this.basicuploader = new Y.UploaderHTML5(cfg);
                this.basicuploader.render("#basicuploader");

                Y.Assert.areEqual(this.basicuploader.get('enabled'), true);

                this.basicuploader.set("enabled", false);

                Y.Assert.areEqual(this.basicuploader.get('enabled'), false);

                this.basicuploader.set("enabled", true);

                Y.Assert.areEqual(this.basicuploader.get('enabled'), true);
            },

            testMultiple : function () {
                var cfg = {
                  tabIndex: "0",
                  width: "100%",
                  height: "100%",
                  dragAndDropArea: "#droparea",
                  enabled: true
                };

                this.basicuploader = new Y.UploaderHTML5(cfg);
                this.basicuploader.render("#basicuploader");

                this.basicuploader.set("multipleFiles", true);
                Y.Assert.areEqual(this.basicuploader.get('multipleFiles'), true);

                this.basicuploader.set("multipleFiles", false);
                Y.Assert.areEqual(this.basicuploader.get('multipleFiles'), false);
            },

            testUpdateList : function () {
                var cfg = {
                  tabIndex: "0",
                  width: "100%",
                  height: "100%",
                  dragAndDropArea: "#droparea"
                };

                this.basicuploader = new Y.UploaderHTML5(cfg);
                this.basicuploader.render("#basicuploader");

                Y.FileHTML5.prototype.isValidFile = function () { return true; };
                Y.FileHTML5.prototype.canUpload = function () { return true; };

                var filearray = [];

                for (var i = 0; i <= 10; i++) {
                    filearray.push({name: i + ".jpg", size: i*1000, type: "jpg"});
                }

                var e = new Y.DOMEventFacade({type:'filelist', preventDefault: function () {}, stopPropagation: function () {}});

                e.target = {};

                e.target.getDOMNode = function () {
                    return {files: filearray};
                }

                this.basicuploader._updateFileList(e);

                this.basicuploader.set("appendNewFiles", false);

                this.basicuploader._updateFileList(e);
            },

            testUploadEvent : function () {
                var cfg = {
                  tabIndex: "0",
                  width: "100%",
                  height: "100%",
                  dragAndDropArea: "#droparea"
                };

                this.basicuploader = new Y.UploaderHTML5(cfg);
                this.basicuploader.render("#basicuploader");

                var e = new Y.DOMEventFacade({type:'file:uploadstart', preventDefault: function () {}, stopPropagation: function () {}});

                this.basicuploader._uploadEventHandler(e);

                e.type = "file:uploadprogress";
                this.basicuploader._uploadEventHandler(e);

                e.type = "uploaderqueue:totaluploadprogress";
                this.basicuploader._uploadEventHandler(e);

                e.type = "file:uploadcomplete";
                this.basicuploader._uploadEventHandler(e);

                e.type = "uploaderqueue:alluploadscomplete";
                this.basicuploader._uploadEventHandler(e);

                e.type = "uploaderqueue:uploaderror";
                this.basicuploader._uploadEventHandler(e);
            },

            testOpenFileSelect : function () {
               var cfg = {
                  tabIndex: "0",
                  width: "100%",
                  height: "100%",
                  dragAndDropArea: "#droparea"
                };

                this.basicuploader = new Y.UploaderHTML5(cfg);
                this.basicuploader.render("#basicuploader");

                var domNode = this.basicuploader._fileInputField.getDOMNode();

                domNode.onclick = function (ev) {
                    ev.preventDefault();
                    ev.stopPropagation();
                    return false;
                };

                this.basicuploader._fileInputField.on("click", function (ev) {ev.preventDefault(); ev.stopPropagation(); return false;});

                this.basicuploader.openFileSelectDialog();
            },

            testUploadCalls: function () {
               var cfg = {
                  tabIndex: "0",
                  width: "100%",
                  height: "100%",
                  dragAndDropArea: "#droparea"
                };

                this.basicuploader = new Y.UploaderHTML5(cfg);
                this.basicuploader.render("#basicuploader");


                Y.FileHTML5.prototype.isValidFile = function () { return true; };
                Y.FileHTML5.prototype.canUpload = function () { return true; };

                var filearray = [];

                for (var i = 0; i <= 10; i++) {
                    filearray.push({name: i + ".jpg", size: i*1000, type: "jpg"});
                }

                var e = new Y.DOMEventFacade({type:'filelist', preventDefault: function () {}, stopPropagation: function () {}});

                e.target = {};

                e.target.getDOMNode = function () {
                    return {files: filearray};
                }

                this.basicuploader._updateFileList(e);

                // Only start the uploads if the browser actually supports HTML5 file upload
                if (Y.config.win.FormData) {
                    this.basicuploader.uploadAll("testurl.html");
                    this.basicuploader.upload(new Y.FileHTML5(filearray[0]), "testurl.html", {foo: "bar"});
                }
            }
        });

        var suite = new Y.Test.Suite("Uploader: HTML5");
        suite.add(BasicUploader);

        Y.Test.Runner.add(suite);
        Y.Test.Runner.run();
    });

})();
</script>
</body>
</html>
